package cn.edu.nju.oraldefense.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import cn.edu.nju.oraldefense.model.Teacher;
import cn.edu.nju.oraldefense.service.CommentService;
import cn.edu.nju.oraldefense.service.TeacherService;

import com.opensymphony.xwork2.ActionSupport;

public class TeacherListAction extends ActionSupport {

	private TeacherService teacherService;
	private CommentService commentService;

	private int start;
	private int limit;
	private String yearmonth;
	
	private List<Teacher> root;
	private int totalProperty;

	@JSON(serialize = false)
	public TeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@JSON(serialize = false)
	public CommentService getCommentService() {
		return commentService;
	}

	@Resource
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	

	public String getYearmonth() {
		return yearmonth;
	}

	public void setYearmonth(String yearmonth) {
		this.yearmonth = yearmonth;
	}

	public List<Teacher> getRoot() {
		return root;
	}

	public void setRoot(List<Teacher> root) {
		this.root = root;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public String deanExecute() {
		System.out.println("start:" + start + ";limit:" + limit);
		root = teacherService.getByPage(start, limit);
		totalProperty = teacherService.getCount();
		return SUCCESS;
	}

	public String observerExecute() {
		String ym[] = yearmonth.split("-");
		int year = Integer.parseInt(ym[0]);
		int month = Integer.parseInt(ym[1]);
		yearmonth = yearmonth + "-01";
		
		String nextyearmonth = "";
		if(month==12) {
			year++;
			nextyearmonth = year+"-"+"01"+"-01";
		}else {
			nextyearmonth = year+"-"+(month+1)+"-01";
		}
		
		root = teacherService.getByPage(start, limit);
		for(Teacher teacher:root) {
			teacher.setCommentTime(commentService.getByTeacherAndMonth(teacher, yearmonth,nextyearmonth).size());
		}
		totalProperty = teacherService.getCount();
		return SUCCESS;
	}

}
